package co.com.inventarios.vista;

import java.util.Date;

import co.com.inventarios.logica.RequisicionLogica;
import co.com.inventarios.logica.TrasladoLogica;
import co.com.inventarios.modelo.Bodega;
import co.com.inventarios.modelo.Dlluser;
import co.com.inventarios.modelo.Label;
import co.com.inventarios.modelo.Mngcco;
import co.com.inventarios.modelo.Mngdoc;
import co.com.inventarios.modelo.MngdocId;
import co.com.inventarios.modelo.RequiDoc;
import co.com.inventarios.modelo.RequiDocId;
import co.com.inventarios.modelo.RequiPro;
import co.com.inventarios.utils.SessionUtil;
import co.com.inventarios.utils.SessionVars;

import com.icesoft.faces.component.tree.IceUserObject;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 * <p>
 * The <code>NodeUserObject</code> represents a nodes user object. This
 * particular IceUserobject implementation store extra information on how many
 * times the parent node is clicked on. It is also responsible for copying and
 * delete its self.
 * </p>
 * <p/>
 * <p>
 * In this example pay particularly close attention to the <code>wrapper</code>
 * instance variable on IceUserObject. The <code>wrapper</code> allows for
 * direct manipulations of the parent tree.
 * </p>
 */
public class DynamicNodeUserObject extends IceUserObject {

	// panel stack which will be manipulated when a command links action is
	// fired.
	private TreeBean treeBean;

	private static String nodeToolTip;
	private String enlace;

	// private Requi_Docb_MB requi = new Requi_Docb_MB();
	/**
	 * Default contsructor for a PanelSelectUserObject object. A reference is
	 * made to a backing bean with the name "panelStack", if possible.
	 * 
	 * @param wrapper
	 */
	public DynamicNodeUserObject(DefaultMutableTreeNode wrapper, TreeBean tree,
			String enlace_o) {
		super(wrapper);
		enlace = enlace_o;
		treeBean = tree;
		setLeafIcon("xmlhttp/css/rime/css-images/tree_document.gif");
		setBranchContractedIcon("xmlhttp/css/rime/css-images/tree_folder_close.gif");
		setBranchExpandedIcon("xmlhttp/css/rime/css-images/tree_folder_open.gif");
		setText(generateLabel());
		// setAction("#{tree.Enviar}");
		setTooltip(nodeToolTip);
		setExpanded(true);
	}

	/**
	 * Generates a label for the node based on an incrementing int.
	 * 
	 * @return the generated label (eg. 'Node 5')
	 */
	private String generateLabel() {
		return "Node  " + Integer.toString(treeBean.getIncreasedLabelCount());
	}

	/**
	 * Deletes this not from the parent tree.
	 * 
	 * @param event
	 *            that fired this method
	 */
	public void deleteNode(ActionEvent event) {
		((DefaultMutableTreeNode) getWrapper().getParent())
				.remove(getWrapper());
	}

	/**
	 * Copies this node and adds a it as a child node.
	 * 
	 * @param event
	 *            that fired this method
	 */
	public void copyNode(ActionEvent event) {
		DefaultMutableTreeNode clonedWrapper = new DefaultMutableTreeNode();
		DynamicNodeUserObject clonedUserObject = new DynamicNodeUserObject(
				clonedWrapper, treeBean, "");
		DynamicNodeUserObject originalUserObject = (DynamicNodeUserObject) getWrapper()
				.getUserObject();
		clonedUserObject.setAction(originalUserObject.getAction());
		clonedUserObject.setBranchContractedIcon(originalUserObject
				.getBranchContractedIcon());
		clonedUserObject.setBranchExpandedIcon(originalUserObject
				.getBranchExpandedIcon());
		clonedUserObject.setExpanded(originalUserObject.isExpanded());
		clonedUserObject.setLeafIcon(originalUserObject.getLeafIcon());
		clonedWrapper.setUserObject(clonedUserObject);
		getWrapper().insert(clonedWrapper, 0);
	}

	public String getEnlace() {
		return enlace;
	}

	public void setEnlace(String enlace) {
		this.enlace = enlace;
	}

	/**
	 * Registers a user click with this object and updates the selected node in
	 * the TreeBean.
	 * 
	 * @param event
	 *            that fired this method
	 */
	public void nodeClicked(ActionEvent event) {
		// System.err.println("Hace Click..........");
		this.setExpanded(true);
		treeBean.setSelectedNodeObject(this);
	}

	public String Enviar() {
		if (enlace == "TrasladoR_B") {
			TrasladoR_MB obja = (TrasladoR_MB) FacesUtils
					.getManagedBean("TrasladoR_MB");
			obja.setlistTrasDoc(null);
			obja.action_BuscarDefault(null);
		}
		if (enlace == "Traslado_B") {
			Traslado_MB objb = (Traslado_MB) FacesUtils
					.getManagedBean("Traslado_MB");
			objb.setlistTrasDoc(null);
			objb.action_BuscarDefault(null);
		}
		if (enlace == "Entrada_B") {
			Entradas_MB objc = (Entradas_MB) FacesUtils
					.getManagedBean("Entradas_MB");
			objc.setlistTrasDoc(null);
			objc.action_BuscarDefault(null);
		}
		if (enlace == "Novedad_B") {
			Novedades_MB objd = (Novedades_MB) FacesUtils
					.getManagedBean("Novedades_MB");
			objd.setlistTrasDoc(null);
			objd.action_BuscarDefault(null);
		}

		if (enlace == "Requisicion_B") {
			Requi_Docb_MB obje = (Requi_Docb_MB) FacesUtils
					.getManagedBean("Requi_Docb_MB");
			obje.setlistReqDoc(null);
			obje.action_BuscarDefault(null);
		}
		if (enlace == "RequisicionR_B") {
			Requi_DocR_MB objf = (Requi_DocR_MB) FacesUtils
					.getManagedBean("Requi_DocR_MB");
			objf.setlistReqDoc(null);
			objf.action_BuscarDefault(null);
		}

		if (enlace == "Novedad_P") {

			Novedades_MB obj = (Novedades_MB) FacesUtils
					.getManagedBean("Novedades_MB");
			obj.setAgregarpro(true);
			obj.setSwnuevo(true);
			obj.setSweditando(false);
			obj.setSwcerrado(false);
			obj.setProselec(false);
			obj.setAproducto(null);
			obj.setApronombre(null);
			obj.setAprounidad(null);
			obj.setAprecio(null);
			obj.setAprocantidad(null);
			obj.setListaUnimed(null);
			obj.setBbodegas(null);
			obj.setBbodegasnom(null);
			obj.setSel_unimed(null);
			obj.setMngdoc_c(new Mngdoc());
			obj.setTraslado_logica(new TrasladoLogica());
			obj.setMiccosto(new Mngcco());
			obj.setMiBodegae(new Bodega());
			obj.setMiBodegas(new Bodega());
			obj.setMiDlluser(new Dlluser());
			obj.setMilabel(new Label());
			obj.setMiId(new MngdocId());
			obj.getMiId().setDocclase(obj.getBclase());
			obj.getMiId().setDocempresa(obj.getBempresa());
			obj.getMiId().setDocnumero(0.0);
			obj.getMiccosto().setCcocodigo("CC1 ");
			Date today = new Date();
			obj.setBfecha(today);
			obj.setSelecestado("1");
			obj.getMngdoc_c().setId(obj.getMiId());
			obj.getMngdoc_c().setDocccosto("CC1 ");
			obj.getMngdoc_c().setBodega(obj.getMiBodegae());
			obj.getMngdoc_c().setDlluser(obj.getMiDlluser());
			obj.getMngdoc_c().setLabel(obj.getMilabel());
			obj.setBdetalle(null);
			obj.setBnumero(null);
			obj.setBccosto(null);
			obj.setBbodegae(null);
			obj.setBbodegas((String) SessionUtil.get(SessionVars.BODEGA));
			obj.getSel_document();
			// obj.action_validadoc(null);
			obj.setBusuario((String) SessionUtil.get(SessionVars.USER));

			int fin;
			try {
				obj.action_Validabodega(null);
				obj.action_Validausuario();
				String numero = obj.getTraslado_logica().MngDocnum_Id(
						obj.getBclase(),
						obj.getSel_document()[0].getValue().toString())
						.toString();
				fin = numero.toString().indexOf(".");
				numero = numero.toString().substring(0, fin);

				obj.setBnumero(numero);
			} catch (Exception e) {
				e.printStackTrace();
			}
			obj.setlistDetalle(null);
			obj.setMngmcnw(null);
		}
		
		if (enlace == "Entrada_P") {

			Entradas_MB obj = (Entradas_MB) FacesUtils
					.getManagedBean("Entradas_MB");
			obj.setAgregarpro(true);
			obj.setSwnuevo(true);
			obj.setSweditando(false);
			obj.setSwcerrado(false);
			obj.setProselec(false);
			obj.setAproducto(null);
			obj.setApronombre(null);
			obj.setAprounidad(null);
			obj.setAprecio(null);
			obj.setAprocantidad(null);
			obj.setListaUnimed(null);
			obj.setBbodegas(null);
			obj.setBbodegasnom(null);
			obj.setSel_unimed(null);
			obj.setMngdoc_c(new Mngdoc());
			obj.setTraslado_logica(new TrasladoLogica());
			obj.setMiccosto(new Mngcco());
			obj.setMiBodegae(new Bodega());
			obj.setMiBodegas(new Bodega());
			obj.setMiDlluser(new Dlluser());
			obj.setMilabel(new Label());
			obj.setMiId(new MngdocId());
			obj.getMiId().setDocclase(obj.getBclase());
			obj.getMiId().setDocempresa(obj.getBempresa());
			obj.getMiId().setDocnumero(0.0);
			obj.getMiccosto().setCcocodigo("CC1 ");
			Date today = new Date();
			obj.setBfecha(today);
			obj.setSelecestado("1");
			obj.getMngdoc_c().setId(obj.getMiId());
			obj.getMngdoc_c().setDocccosto("CC1 ");
			obj.getMngdoc_c().setBodega(obj.getMiBodegae());
			obj.getMngdoc_c().setDlluser(obj.getMiDlluser());
			obj.getMngdoc_c().setLabel(obj.getMilabel());
			obj.setBdetalle(null);
			obj.setBnumero(null);
			obj.setBccosto(null);
			obj.setBbodegae(null);
			obj.setBbodegas((String) SessionUtil.get(SessionVars.BODEGA));
			obj.getSel_document();
			// obj.action_validadoc(null);
			obj.setBusuario((String) SessionUtil.get(SessionVars.USER));

			int fin;
			try {
				obj.action_Validabodega(null);
				obj.action_Validausuario();
				String numero = obj.getTraslado_logica().MngDocnum_Id(
						obj.getBclase(),
						obj.getSel_document()[0].getValue().toString())
						.toString();
				fin = numero.toString().indexOf(".");
				numero = numero.toString().substring(0, fin);

				obj.setBnumero(numero);
			} catch (Exception e) {
				e.printStackTrace();
			}
			obj.setlistDetalle(null);
			obj.setMngmcnw(null);
		}

		

		if (enlace == "Traslado_P") {
			Traslado_MB obj = (Traslado_MB) FacesUtils
					.getManagedBean("Traslado_MB");
			obj.setAgregarpro(true);
			obj.setSwnuevo(true);
			obj.setSweditando(false);
			obj.setSwcerrado(false);
			obj.setProselec(false);
			obj.setAproducto(null);
			obj.setApronombre(null);
			obj.setAprounidad(null);
			obj.setBbodegaenom(null);
			obj.setAprecio(null);
			obj.setAprocantidad(null);
			obj.setListaUnimed(null);
			obj.setBbodegas(null);
			obj.setBbodegasnom(null);
			obj.setSel_unimed(null);
			obj.setMngdoc_c(new Mngdoc());
			obj.setTraslado_logica(new TrasladoLogica());
			obj.setMiccosto(new Mngcco());
			obj.setMiBodegae(new Bodega());
			obj.setMiBodegas(new Bodega());
			obj.setMiDlluser(new Dlluser());
			obj.setMilabel(new Label());
			obj.setMiId(new MngdocId());
			obj.getMiId().setDocclase(obj.getBclase());
			obj.getMiId().setDocempresa(obj.getBempresa());
			obj.getMiId().setDocnumero(0.0);
			obj.getMiccosto().setCcocodigo("CC1 ");
			Date today = new Date();
			obj.setBfecha(today);
			obj.setSelecestado("1");
			obj.getMngdoc_c().setId(obj.getMiId());
			obj.getMngdoc_c().setDocccosto("CC1 ");
			obj.getMngdoc_c().setBodega(obj.getMiBodegae());
			obj.getMngdoc_c().setDlluser(obj.getMiDlluser());
			obj.getMngdoc_c().setLabel(obj.getMilabel());
			obj.setBdetalle(null);
			obj.setBnumero(null);
			obj.setBccosto(null);
			obj.setBbodegae(null);
			obj.setBbodegas((String) SessionUtil.get(SessionVars.BODEGA));
			obj.getSel_document();
			// obj.action_validadoc(null);
			obj.setBusuario((String) SessionUtil.get(SessionVars.USER));

			int fin;
			try {
				obj.action_Validabodega(null);
				obj.action_Validausuario();
				String numero = obj.getTraslado_logica().MngDocnum_Id(
						obj.getBclase(),
						obj.getSel_document()[0].getValue().toString())
						.toString();
				fin = numero.toString().indexOf(".");
				numero = numero.toString().substring(0, fin);

				obj.setBnumero(numero);
			} catch (Exception e) {
				e.printStackTrace();
			}
			obj.setlistDetalle(null);
			obj.setMngmcnw(null);
		}

		
		
		
		if (enlace == "Requisicion_P") {
			Requi_Docb_MB obj = (Requi_Docb_MB) FacesUtils
					.getManagedBean("Requi_Docb_MB");
			obj.setSweditando(false);
			obj.setAgregarpro(true);
			obj.setSwnuevo(true);
			obj.setSwcerrado(false);
			obj.setProselec(false);
			obj.setAproducto(null);
			obj.setApronombre(null);
			obj.setBbodegaenom(null);
			obj.setBfechaenv(null);
			obj.setAprounidad(null);
			obj.setAprecio(null);
			obj.setAprocantidad(null);
			obj.setListaUnimed(null);
			obj.setSel_unimed(null);
			obj.setMiBodegae(new Bodega());
			obj.setMiBodegas(new Bodega());
			obj.setMiccosto(new Mngcco());
			obj.setMiDlluser(new Dlluser());
			obj.getMiccosto().setCcocodigo("CC1 ");
			obj.setMiId(new RequiDocId());
			obj.getMiId().setReqclase(obj.getBclase());
			obj.getMiId().setReqempresa(obj.getBempresa());
			obj.getMiId().setReqnumero(0.0);
			obj.setReqDoc_logica(new RequisicionLogica());
			obj.setRequisicion(new RequiDoc());
			obj.setMilabel(new Label());
			Date today = new Date();
			obj.getRequisicion().setId(obj.getMiId());
			obj.getRequisicion().setMngcco(obj.getMiccosto());
			obj.getRequisicion().setBodega(obj.getMiBodegae());
			obj.getRequisicion().setDlluser(obj.getMiDlluser());
			obj.getRequisicion().setLabel(obj.getMilabel());
			obj.setBfecha(today);
			obj.setBdetalle(null);
			obj.setBnumero(null);
			obj.setBccosto(null);
			obj.setBbodegae(null);
			obj.setBbodegas((String) SessionUtil.get(SessionVars.BODEGA));
			obj.setBfechaenv(null);
			obj.setBbodegasnom(null);
			obj.setBusuario((String) SessionUtil.get(SessionVars.USER));
			obj.setSelecestado("2");

			int fin;
			try {
				obj.action_Validabodega(null);
				obj.action_Validausuario(null);

				fin = obj.getReqDoc_logica().MngDocnum_Id(obj.getBclase(),
						obj.getBtipo()).toString().indexOf(".");
				String numero = obj.getReqDoc_logica().MngDocnum_Id(
						obj.getBclase(), obj.getBtipo()).toString().substring(
						0, fin);
				obj.setBnumero(numero);
			} catch (Exception e) {
				e.printStackTrace();
			}
			obj.setlistReqPro(null);
			obj.setRequipro(null);

		}
		
		if (enlace == "Inf_inv_Kardex") {
			Inv_Kardex_MB obj = (Inv_Kardex_MB) FacesUtils
			.getManagedBean("Inv_Kardex_MB");
			obj.setBbodegas((String) SessionUtil.get(SessionVars.BODEGA));
			obj.setlistDetalle(null);
			obj.setMngmcnw(null);
			try {
				obj.action_Validabodega(null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (enlace == "Inf_inv_x_Bodega") {
			Requi_Docb_MB obj = (Requi_Docb_MB) FacesUtils
			.getManagedBean("Requi_Docb_MB");
			obj.setBbodegas((String) SessionUtil.get(SessionVars.BODEGA));
			obj.setListProbodega(null);
			try {
				obj.action_Validabodega(null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		
		
		
		return enlace;
	}

}
